package top.wangjiu.java.util;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class UnsafeFactory {

    private static final Unsafe unsafe;

    static {
        Unsafe unsafe1;
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            unsafe1 = (Unsafe) theUnsafe.get(Unsafe.class);
            theUnsafe.setAccessible(false);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
            unsafe1 = null;
        }
        unsafe = unsafe1;
    }

    public static Unsafe getUnsafe(){
        return unsafe;
    }
}
